/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.AnnouncementGroup;
import com.yuhanginfo.modules.sys.service.AnnouncementGroupService;

/**
 * 公告分类Controller
 * @author Y.j
 * @version 2017-09-13
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/system/announcementGroup")
public class AnnouncementGroupController extends BaseController {

	@Autowired
	private AnnouncementGroupService announcementGroupService;
	
	@ModelAttribute
	public AnnouncementGroup get(@RequestParam(required=false) String id) {
		AnnouncementGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = announcementGroupService.get(id);
		}
		if (entity == null){
			entity = new AnnouncementGroup();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:announcementGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(AnnouncementGroup announcementGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AnnouncementGroup> page = announcementGroupService.findPage(new Page<AnnouncementGroup>(request, response), announcementGroup); 
		model.addAttribute("page", page);
		return "modules/sys/system/announcementGroupList";
	}

	@RequiresPermissions("sys:announcementGroup:view")
	@RequestMapping(value = "form")
	public String form(AnnouncementGroup announcementGroup, Model model) {
		model.addAttribute("announcementGroup", announcementGroup);
		return "modules/sys/system/announcementGroupForm";
	}

	@RequiresPermissions("sys:announcementGroup:edit")
	@RequestMapping(value = "save")
	public String save(AnnouncementGroup announcementGroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, announcementGroup)){
			return form(announcementGroup, model);
		}
		announcementGroupService.save(announcementGroup);
		addMessage(redirectAttributes, "保存公告分类成功");
		return "redirect:"+Global.getAdminPath()+"/sys/system/announcementGroup/?repage";
	}
	
	@RequiresPermissions("sys:announcementGroup:edit")
	@RequestMapping(value = "delete")
	public String delete(AnnouncementGroup announcementGroup, RedirectAttributes redirectAttributes) {
		announcementGroupService.delete(announcementGroup);
		addMessage(redirectAttributes, "删除公告分类成功");
		return "redirect:"+Global.getAdminPath()+"/sys/system/announcementGroup/?repage";
	}
	
	/**
	 * 公告分类是否被公告引用
	 */
	@RequiresPermissions("sys:announcementGroup:edit")
	@RequestMapping(value = "announcementGroupByAnnouncement")
	@ResponseBody
	public Boolean announcementGroupByAnnouncement(AnnouncementGroup announcementGroup, Model model, RedirectAttributes redirectAttributes) {
		return announcementGroupService.announcementGroupByAnnouncement(announcementGroup.getId());
	}
	
	/**
	 * 验证标题名是否重复
	 * @param oldName
	 * @param name
	 * @return
	 */
	@RequiresPermissions("sys:announcementGroup:view")
	@ResponseBody
	@RequestMapping(value = "checkTitle")
	public String checkTitle(String oldTitle,String title) {
		if (title !=null && title.equals(oldTitle)) {
			return "true";
		}else if (title!=null && announcementGroupService.getAnnouncementGroupByName(title)) {
			return "false";
		} 
		return "true";
	}

}